﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace Sun.WPF.Controls
{
    public class SunCalender : Calendar
    {
        /// <summary>
        /// 文本显示内容
        /// </summary>
        TextBlock textBlock;
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            
            //选择按钮
            Button selectButton = this.Template.FindName("PART_SelectButton", this) as Button;
            selectButton.Click += SelectButton_Click;

            //清除按钮
            Button clearButton = this.Template.FindName("PART_ClearButton" , this) as Button;
            clearButton.Click += ClearButton_Click;

            
            textBlock = this.Template.FindName("PART_TextBlock", this) as TextBlock;
            textBlock.FocusableChanged += ContentPresenter_FocusableChanged;
        }

        private void ContentPresenter_FocusableChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            IsDropDwonOpen = true;
        }

        /// <summary>
        /// 清除选择的日期
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ClearButton_Click(object sender, RoutedEventArgs e)
        {
            if(textBlock != null)
            {
                this.SelectedDates.Clear();
                IsDropDwonOpen = false;
                textBlock.Text = string.Empty;
            }
        }

        /// <summary>
        /// 确定选择的日期
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SelectButton_Click(object sender, RoutedEventArgs e)
        {
            if(textBlock != null)
            {
                IsDropDwonOpen = false;

                string text = string.Join(DateTimeSeparator, this.SelectedDates.Select(w => w.ToString(DateTimeFormat)));

                textBlock.ToolTip = text.Replace(DateTimeSeparator, Environment.NewLine);
                textBlock.Text = text;

                if(Command != null)
                {
                    Command.Execute(null);
                }
            }
        }

        /// <summary>
        /// 显示隐藏控制
        /// </summary>
        public bool IsDropDwonOpen
        {
            get { return (bool)GetValue(IsDropDwonOpenProperty); }
            set { SetValue(IsDropDwonOpenProperty, value); }

        }

        public static readonly DependencyProperty IsDropDwonOpenProperty = DependencyProperty.Register("IsDropDwonOpen", 
            typeof(bool), typeof(SunCalender), new FrameworkPropertyMetadata());

        /// <summary>
        /// 显示格式
        /// </summary>
        public string DateTimeFormat
        {
            get { return (string)GetValue(DateTimeFormatProperty); }
            set { SetValue(DateTimeFormatProperty, value); }

        }

        public static readonly DependencyProperty DateTimeFormatProperty = DependencyProperty.Register("DateTimeFormat",
            typeof(string), typeof(SunCalender), new FrameworkPropertyMetadata());

        /// <summary>
        /// 分隔符
        /// </summary>
        public string DateTimeSeparator
        {
            get { return (string)GetValue(DateTimeSeparatorProperty); }
            set { SetValue(DateTimeSeparatorProperty, value); }

        }

        public static readonly DependencyProperty DateTimeSeparatorProperty = DependencyProperty.Register("DateTimeSeparator",
            typeof(string), typeof(SunCalender), new FrameworkPropertyMetadata());

        /// <summary>
        /// 下拉按钮图标
        /// </summary>
        public string DropDwonButtonIcon
        {
            get { return (string)GetValue(DropDwonButtonIconProperty); }
            set { SetValue(DropDwonButtonIconProperty, value); }

        }

        public static readonly DependencyProperty DropDwonButtonIconProperty = DependencyProperty.Register("DropDwonButtonIcon",
            typeof(string), typeof(SunCalender), new FrameworkPropertyMetadata());

        /// <summary>
        /// 选择命令
        /// </summary>
        public ICommand Command
        {
            get
            {
                return (ICommand)GetValue(CommandProperty);
            }
            set
            {
                SetValue(CommandProperty, value);
            }
        }

        public static readonly DependencyProperty CommandProperty =
                DependencyProperty.Register("Command", typeof(ICommand), typeof(SunCalender));
    }
}
